#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int n;
priority_queue<int,vector<int>,greater<int>> qu;

int main(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		int x;
		cin >> x;
		qu.push(x);
	}
	long long ret = 0;
	while(qu.size() > 1){
		int x = qu.top();
		qu.pop();
		int y = qu.top();
		qu.pop();
		ret += x + y;
		qu.push(x + y);
	}
	cout << ret << endl;
	return 0;
}
